﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;
using PH.Model.Achievements;
using PH.Logic.Achievements;
using PH.Model.Results;

namespace PH.Web.Management.Areas.Achievements.Controllers
{
    [RouteArea("Achivevments")]
    [RoutePrefix("Mark")]
    public class MarkApiController : ApiController
    {
        [Route("ImportMark")]
        [HttpPost]
        public JsonResult<bool> ImportMark(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                var fGuid = request.Value<string>("fGuid");
                var sGuid = request.Value<string>("sGuid");
                var aGuid=request.Value<string>("aGuid");
                
                if (string.IsNullOrEmpty(fGuid))
                    throw new Utils.PhException("没有找到上传的文件");
                if (string.IsNullOrEmpty(sGuid))
                    throw new Utils.PhException("无效的学校编号");
                if (string.IsNullOrEmpty(aGuid))
                    throw new Utils.PhException("无效的档案");

                var fileGuid=new Guid(fGuid);
                var schoolGuid=new Guid(sGuid);
                var archiveGuid=new Guid(aGuid);

                var eGuid = MarkLogic.ImportMark(fileGuid, archiveGuid, schoolGuid);
                
                if (eGuid==Guid.Empty)
                    return new JsonResult<bool>(true);

                errinfo = string.Format("导入成绩数据有错误，请下载结果文件查看详情。<div style='text-align:center; padding:10px'><a href='/Files/DownLoad/{0}' target='_blank'>详情请点击下载Excel文件</a></div>", eGuid);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        //[Route("GetMarkStatisticByClass")]
        //[HttpPost]
        //public JsonEntitySet<VMarkStatisticByClassModel> GetMarkStatisticByClass(JObject request)
        //{
        //    string errinfo = "no data";
        //    try
        //    {
        //        MarkQuery sea = new MarkQuery();
        //        sea.PageIndex = request.Value<int>("pageIndex");
        //        sea.PageSize = request.Value<int>("pageSize");
        //        var aGuid = request.Value<string>("aGuid");
        //        if (!string.IsNullOrEmpty(aGuid))
        //            sea.ArchiveGuid = new Guid(aGuid);
        //        //var sGuid = request.Value<string>("sGuid");
        //        //if (!string.IsNullOrEmpty(sGuid))
        //        //    sea.SchoolGuid = new Guid(sGuid);
        //        sea.SchoolGuid = Web.Global.WebContext.CurrentSchool.Guid;
        //        sea.ClassID = request.Value<int>("classID");
        //        sea.SubjectID = request.Value<int>("subjectID");
        //        var result = MarkLogic.GetMarkStatisticByClass(sea);
        //        return new JsonEntitySet<VMarkStatisticByClassModel>(result);
        //    }
        //    catch (Utils.PhException pex) { errinfo = pex.Message; }
        //    catch (Exception ex)
        //    {
        //        errinfo = ex.Message;
        //        Common.Logger.Error("", ex);
        //    }
        //    return new JsonEntitySet<VMarkStatisticByClassModel>(null, errinfo, false);
        //}
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<MarkModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                MarkQuery sea = new MarkQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.GetSubject = request.Value<bool>("getSubject");
                sea.GetSchoolName = request.Value<bool>("getSchoolName");
                sea.GetClassName = request.Value<bool>("getClassName");
                sea.GetStudentName = request.Value<bool>("getStudentName");

                var aGuid = request.Value<string>("aGuid");
                if (!string.IsNullOrEmpty(aGuid))
                    sea.ArchiveGuid = new Guid(aGuid);
                var sGuid = request.Value<string>("sGuid");
                if (!string.IsNullOrEmpty(sGuid))
                    sea.SchoolGuid = new Guid(sGuid);
                sea.ClassID = request.Value<int>("classID");
                sea.SubjectID = request.Value<int>("subjectID");
                sea.Sex = request.Value<byte>("sex");
                var stuGuid = request.Value<string>("stuGuid");
                if (!string.IsNullOrEmpty(stuGuid))
                    sea.StudentGuid = new Guid(stuGuid);
                sea.AutoPass = request.Value<bool?>("autoPass");
                sea.StudentID = request.Value<int>("studentID");
                sea.OrderBy = new Model.QueryBase.Sort()
                {
                    Field = request.Value<string>("sortField"),
                    Dir = request.Value<string>("sortDir")
                };
                sea.Key = request.Value<string>("key");
                var result = MarkLogic.Gets(sea);
                return new JsonEntitySet<MarkModel>(result);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<MarkModel>(null, errinfo, false);
        }

        //[Route("GetMarkStatisticBySchool")]
        //[HttpPost]
        //public JsonEntitySet<VMarkStatisticBySchoolModel> GetMarkStatisticBySchool(JObject request)
        //{
        //    string errinfo = "no data";
        //    try
        //    {
        //        MarkQuery sea = new MarkQuery();
        //        sea.PageIndex = request.Value<int>("pageIndex");
        //        sea.PageSize = request.Value<int>("pageSize");
        //        var aGuid = request.Value<string>("aGuid");
        //        if (!string.IsNullOrEmpty(aGuid))
        //            sea.ArchiveGuid = new Guid(aGuid);
        //        var sGuid = request.Value<string>("sGuid");
        //        if (!string.IsNullOrEmpty(sGuid))
        //            sea.SchoolGuid = new Guid(sGuid);
        //        sea.SubjectID = request.Value<int>("subjectID");
        //        var result = MarkLogic.GetMarkStatisticBySchool(sea);
        //        return new JsonEntitySet<VMarkStatisticBySchoolModel>(result);
        //    }
        //    catch (Utils.PhException pex) { errinfo = pex.Message; }
        //    catch (Exception ex)
        //    {
        //        errinfo = ex.Message;
        //        Common.Logger.Error("", ex);
        //    }
        //    return new JsonEntitySet<VMarkStatisticBySchoolModel>(null, errinfo, false);
        //}
        
        [Route("Update")]
        [HttpPost]
        public JsonResult<bool> Update(JObject request)
        {
            string errinfo = "对不起！更新失败，出错原因：";
            try
            {
                var id = request.Value<int>("id");
                var newmark = request.Value<string>("newmark");

                MarkLogic.Update(id, newmark);
                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }

        [Route("CopyMarks")]
        [HttpPost]
        public JsonResult<bool> CopyMarks(JObject request)
        {
            string errinfo = "对不起！更新失败，出错原因：";
            try
            {
                var sGuidStr = request.Value<string>("sGuid");
                var tGuidStr = request.Value<string>("tGuid");

                Guid sGuid = Guid.Empty, tGuid = Guid.Empty, schGuid = Guid.Empty;
                if (!string.IsNullOrEmpty(sGuidStr))
                    sGuid = new Guid(sGuidStr);
                if (!string.IsNullOrEmpty(tGuidStr))
                    tGuid = new Guid(tGuidStr);

                MarkLogic.CopyMarks(sGuid, tGuid, Web.Global.WebContext.CurrentSchool.Guid);
                return new JsonResult<bool>(true);
            }
            catch (Utils.PhException pex) { errinfo = pex.Message; }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
